//
//  dma.cpp
//  继承和动态分配
//
//  Created by XiongGuang on 16/8/11.
//  Copyright © 2016年 XiongGuang. All rights reserved.
//

#include "dma.hpp"
#include <cstring>

//baseDMA methods

baseDMA::baseDMA(const char * l,int r)
{
    label=new char[std::strlen(l)+1];
    std::strcpy(label, l);
    rating=r;
}

baseDMA::baseDMA(const baseDMA & rs)
{
    label=new char[std::strlen(rs.label)+1];
    std::strcpy(label, rs.label);
    rating=rs.rating;
}

baseDMA::~baseDMA()
{
    delete [] label;
}

baseDMA & baseDMA::operator=(const baseDMA &rs)
{
    if(this==&rs)
        return *this;
    delete [] label;
    
    label=new char[std::strlen(rs.label)+1];
    std::strcpy(label, rs.label);
    rating=rs.rating;
    return *this;
}

std::ostream & operator<<(std::ostream & os,const baseDMA & rs)
{
    os<<"Label :"<<rs.label<<std::endl;
    os<<"Rating :"<<rs.rating<<std::endl;
    return os;
}

//lackDMA methods

lackDMA::lackDMA(const char * c,const char * l,int r):baseDMA(l,r)
{
    std::strncpy(color, c, 3);
    color[3]='\0';
}

lackDMA::lackDMA(const char * c,const baseDMA & rs):baseDMA(rs)
{
    std::strncpy(color, color, COL_LEN-1);
    color[COL_LEN-1]='\0';
}

std::ostream & operator<<(std::ostream & os,const lackDMA & ls)
{
   
    os<<(baseDMA &) ls;
    os<<"Color: " << ls.color<<std::endl;
    ///////////////////////////////////???????????
    
    return os;
    
}

//hasDMA methods
hasDMA::hasDMA(const char * s,const char * l,int r):baseDMA(l,r)
{
    style=new char [std::strlen(s)+1];
    std::strcpy(style,s);
}

hasDMA::hasDMA(const char * s,const baseDMA & rs):baseDMA(rs)
{
    style=new char[std::strlen(s)+1];
    std::strcpy(style, s);
}

hasDMA::hasDMA(const hasDMA & hs):baseDMA(hs)
{
    style=new char [std::strlen(hs.style)+1];
    strcpy(style, hs.style);
}

hasDMA::~hasDMA()
{
    delete [] style;
}

hasDMA & hasDMA::operator=(const hasDMA & hs)
{
    if(this==&hs)
        return *this;
    baseDMA::operator=(hs);
    style=new char [std::strlen(hs.style)+1];
    //delete [] style;---------------------------->未曾创建style ,不存在该指针
    std::strcpy(style, hs.style);
    
    return *this;
}
std::ostream & operator<<(std::ostream & os, const hasDMA & hs)
{
    os<<(const baseDMA &)hs;
    os<<"Style: "<<hs.style<<std::endl;
    return os;
}

